import Vue from 'vue'
import { isBlob } from '@/utils/validate'
/**
 * @param {Sting} stream-/backend reponse data/
 * @param {Sting} name-// the file name
 * @param {String} suffix--// the extension name
 */
const fileDownload = (stream, name, suffix = '.xls') => {
  if (stream && name) {
    const blob = isBlob(stream) ? stream : new Blob([stream])
    const fullName = `${name + suffix}`
    // IE10+ 浏览器特殊处理
    if (window.navigator.msSaveBlob) {
      window.navigator.msSaveBlob(blob, fullName)
    } else {
      const href = window.URL.createObjectURL(blob)
      const a = document.createElement('a')
      a.href = href
      a.download = fullName
      document.body.appendChild(a)
      a.click()
      window.URL.revokeObjectURL(href)
      document.body.removeChild(a)
    }
  }
}

Vue.prototype.fileDownload = fileDownload

export default fileDownload
